package cn.yll.api.conf;

import cn.yll.common.constant.MyConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * swagger2.0 配置
 * 文档地址：ip:port/doc.html
 * @author yangll
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Value("${spring.application.name}")
    private String appName;

    @Bean(value = "swagger2-api")
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security())
                .ignoredParameterTypes(HttpServletRequest.class, HttpSession.class, HttpServletResponse.class);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("网上商城系统" + appName + "项目接口文档").version("1.0")
                .description("部分接口需要登录态，先调用登录相关的接口，登录成功后就可以调用这些接口了")
                .contact(new Contact("杨磊磊", "", "15210831334@163.com")).build();
    }

    private List<SecurityScheme> security() {
        return Arrays.asList(
                new ApiKey(MyConstants.TOKEN_KEY, MyConstants.TOKEN_KEY, "header")
        );
    }

}
